﻿using System;
using System.Collections.Specialized;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

namespace Sketchial.Data
{
    public class BlobHelper
    {

        //unused
        public static CloudBlobContainer GetContainer(string accountName, string containerName)
        {
            // Get a handle on account, create a blob service client and get container proxy
            var account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(accountName));
            var client = account.CreateCloudBlobClient();

            return client.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue(containerName));
        }
        //unused


        public static CloudStorageAccount GetStorageAccount()
        {
            return CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
        }

        private static void EnsureContainerExists(string containerName)
        {
            var container = GetStorageAccount().CreateCloudBlobClient().GetContainerReference(containerName);
            container.CreateIfNotExist(new BlobRequestOptions()
              {
                  UseFlatBlobListing = true,
                  BlobListingDetails = BlobListingDetails.All
              });

            var permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            container.SetPermissions(permissions);
        }


        public static bool UploadImage(string id,
            string name,
            string description,
            string tags,
            string fileName,
            string contentType,
            byte[] data,
            string containerName)
        {
            try
            {
                // Create a blob in container and upload image bytes to it
                EnsureContainerExists(containerName);
                var blob = GetStorageAccount().CreateCloudBlobClient().GetContainerReference(containerName).GetBlobReference(name);
                
                blob.Properties.ContentType = contentType;

                // Create some metadata for this image
                var metadata = new NameValueCollection();
                metadata["Id"] = id;
                metadata["Filename"] = fileName;
                metadata["ImageName"] = String.IsNullOrEmpty(name) ? "undefined" : name;
                metadata["Description"] = String.IsNullOrEmpty(description) ? "undefined" : description;
                metadata["Tags"] = String.IsNullOrEmpty(tags) ? "undefined" : tags;

                // Add and commit metadata to blob
                blob.Metadata.Add(metadata);
                blob.UploadByteArray(data);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public static CloudBlob GetAnImage(string accountName,
            string containerName,
            string blobName)
        {
            var container = GetContainer(accountName, containerName);
            return container.GetBlobReference(blobName);
        }

    }
}